package com.hinnotek.amoeba.ui.activity.old

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.RelatePurchaseListAdapter
import com.hinnotek.amoeba.bean.old.RelatePurchasingBean
import com.hinnotek.amoeba.databinding.ActivityRelatePurchasListBinding
import com.hinnotek.amoeba.ext.init
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.StatusBarUtil
import com.hinnotek.amoeba.utilities.ToastUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import java.util.*

class RelatePurchasingListActivity : BaseActivity<ActivityRelatePurchasListBinding>() {

    private var mAdapter: RelatePurchaseListAdapter? = null
    private  var stringList = mutableListOf<RelatePurchasingBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_relate_purchas_list)
        setLeftVisible(true)
        setTitle("采购")

    }

    override fun initVew() {
        StatusBarUtil.setStatusBarColor(this, R.color.colorbtns)
        bindingView.recyclerView.init()
        bindingView.recyclerView.setAdapter(RelatePurchaseListAdapter().also { mAdapter = it })
        mAdapter?.setOnItemClickListener { adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int ->
            IntentUtils.startActivity(
                mContext,
                PurchaseOrderActivity::class.java
            )
        }
        mAdapter?.setIdCallBack(object : RelatePurchaseListAdapter.ClickCallBack {
            override fun onClick(bean: RelatePurchasingBean?, position: Int) {
                for (i in stringList.indices) {
                    val bean1 = stringList[i]
                    bean1.b = 0
                }
                bean?.b = 1
                mAdapter?.notifyDataSetChanged()
            }
        })
        findViewById<View>(R.id.rb_purchase_true).setOnClickListener {
            for (bean in stringList) {
                if (bean.b == 1) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("id", bean.id))
                    finish()
                    return@setOnClickListener
                }
            }
            finish()
        }
    }


    override fun setData() {
        for (i in 0..9) {
            val bean = RelatePurchasingBean(0,i.toString() + "")

            stringList.add(bean)
        }
        mAdapter?.setList(stringList)

        //下拉刷新
        bindingView.refreshLayout.setOnRefreshListener { refreshLayout: RefreshLayout ->
            refreshLayout.layout.postDelayed(
                {
                    val status = refreshStatus
                    when (status) {
                        Status.SUCCESS -> {
                            mAdapter?.setList(stringList)
                            bindingView.refreshLayout.resetNoMoreData() //setNoMoreData(false);
                            bindingView.llStateful.showContent()
                            bindingView.refreshLayout.setEnableLoadMore(true)
                        }
                        Status.EMPTY -> {
                            bindingView.llStateful.showEmpty()
                            bindingView.refreshLayout.setEnableLoadMore(false)
                        }
                        Status.ERROR -> showError()
                        Status.NO_NET -> showOffline()
                        else -> {
                        }
                    }
                    refreshLayout.finishRefresh()
                }, 0
            )
        }
        //上拉加载
        bindingView.refreshLayout.setOnLoadMoreListener { refreshLayout: RefreshLayout ->
            refreshLayout.layout.postDelayed(
                {
                    if (mAdapter!!.getItemCount() > 30) {
                        ToastUtils.showShort("数据全部加载完毕")
                        refreshLayout.finishLoadMoreWithNoMoreData() //将不会再次触发加载更多事件
                    } else {
                        mAdapter?.addData(stringList)
                        refreshLayout.finishLoadMore()
                    }
                }, 0
            )
        }
        bindingView.refreshLayout.autoRefresh() //第一次进入触发自动刷新，演示效果
    }

    private fun showOffline() {
        bindingView.llStateful.showOffline(View.OnClickListener { v: View? -> bindingView.refreshLayout.autoRefresh() })
        bindingView.refreshLayout.setEnableLoadMore(false)
    }

    private fun showError() {
        bindingView.llStateful.showError(View.OnClickListener { v: View? -> bindingView.refreshLayout.autoRefresh() })
        bindingView.refreshLayout.setEnableLoadMore(false)
    }

    private enum class Status {
        SUCCESS, EMPTY, ERROR, NO_NET
    }

    private val refreshStatus: Status
        private get() {
            val status = (Math.random() * 10).toInt()
            return if (status % 2 == 0) {
                Status.SUCCESS
            } else if (status % 3 == 0) {
                Status.EMPTY
            } else if (status % 5 == 0) {
                Status.ERROR
            } else {
                Status.NO_NET
            }
        }
}